const { init } = require('./../../wxCloudClientSDK.umd.js')
// 指定云开发环境 ID
wx.cloud.init({
  env: "cloud1-9gel6ndh8e555155", // 当前的云开发环境 ID
});
const client = init(wx.cloud);

Page({
  data: {
    userPoints: 1280,
    showRulesPopup: false,
    pointsRules: [
      {
        id: 1,
        title: '签到规则',
        description: '每日签到可获得10积分，连续签到7天额外奖励30积分'
      },
      {
        id: 2,
        title: '消费规则',
        description: '每消费1元可获得1积分'
      },
      {
        id: 3,
        title: '积分有效期',
        description: '积分有效期为一年，请及时使用'
      }
    ],
    currentTab: 1, // 当前选中标签
    featuredGoods: [],
    exchangeableGoods: [],

    categories: [
      { id: 1, name: '精选商品' },
      { id: 2, name: '我可兑换' },
    ],
    currentCategory: 1,
  },

  onLoad: function() {

    this.querySelectedGoods();
    
  },

  querySelectedGoods() {

    // 获取积分的精选商品
   client.models.goods_info.list({
    filter: {
      where: {
       $and: [{
         category: {
           $eq: 'selectedGoods'
         },
       },
     ],
      }
    },
    pageSize: 10,
    pageNumber: 1,
    getCount: true
  }).then(res => {
   console.log("goods_info: " + res.data.records);
   this.parseSelectedGoods(res.data.records);
 }).catch(res => {
   console.log("goods_info err: " + res)
 });

  },

  parseSelectedGoods(data) {
    const parsedData = data.map(item => {
      return {
        id: item._id,
        name: item.goodsName,
        desc: item.description,
        points: item.price,
        imageUrl: item.thumbnailUrl,
      };
    });

    this.setData({
      featuredGoods: parsedData
    });

  },

  toGoodsDetail(event) {
    const id = event.currentTarget.dataset.param1;
    wx.navigateTo({
      url: '/page/goodsdetail/index?param1=' + id,
    });
  },

  // 切换商品分类
  switchTab(e) {
    const tabIndex = parseInt(e.currentTarget.dataset.tab);
    this.setData({
      currentTab: tabIndex
    });

    if (tabIndex === 2) {
      this.queryExchangeGoods();
    }
  },

  queryExchangeGoods() {
    // 获取积分的精选商品
   client.models.goods_info.list({
        filter: {
          where: {
          $and: [{
            category: {
              $eq: 'selectedGoods'
            },
          }, {
            price: {
              $lte: 100
            }
          }
        ],
          }
        },
        pageSize: 10,
        pageNumber: 1,
        getCount: true
      }).then(res => {
        console.log("goods_info: " + res.data.records);
        this.parseExchangeGoods(res.data.records);
    }).catch(res => {
      console.log("goods_info err: " + res)
    });
  },

  parseExchangeGoods(data) {
    const parsedData = data.map(item => {
      return {
        id: item._id,
        name: item.goodsName,
        desc: item.description,
        points: item.price,
        url: item.thumbnailUrl,
      };
    });

    this.setData({
      exchangeableGoods: parsedData
    });
  },

  showRules() {
    this.setData({
      showRulesPopup: true
    });
  },

  hideRules() {
    this.setData({
      showRulesPopup: false
    });
  },

  completeTask(e) {
    const taskId = e.currentTarget.dataset.id;
    const tasks = this.data.dailyTasks.map(task => {
      if (task.id === taskId && !task.completed) {
        // 完成任务逻辑
        wx.showToast({
          title: '获得' + task.points + '积分',
          icon: 'success'
        });
        return { ...task, completed: true };
      }
      return task;
    });
    this.setData({
      dailyTasks: tasks
    });
  },

  navigateToRecommend() {
    wx.navigateTo({
      url: '/pages/recommend/index'
    });
  },

  navigateToExchange() {//点击积分页面明细
    wx.navigateTo({
      url: '/page/mypoints/index'
    });
  },


  onCategoryChange(e) {
    const id = parseInt(e.currentTarget.dataset.id);
    this.setData({
      currentCategory: id,
      currentTab: id,
    })

    if (id === 2) {
      this.queryExchangeGoods();
    } else {
      this.querySelectedGoods();
    }
  },
});